﻿@using Telerik.Web.Mvc
@using Telerik.Web.Mvc.UI
@using Presentation.Web.Admin
@model  IEnumerable<Infrastructure.Crosscutting.Authorize.Dto.MvcActionDto>
@{
    if (ViewBag.rolemodel != null)
    {
        ViewBag.subtitle = "角色权限--" + ViewBag.rolemodel.Name;
        ViewBag.title = "角色权限--" + ViewBag.rolemodel.Name;
    }
    else
    {
        ViewBag.subtitle = "角色权限";
        ViewBag.title = "角色权限";
    }
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<input type="hidden" value="@(ViewBag.rolemodel.Id)" id="RoleId" name="RoleId" />
@{Html.Telerik().Grid(Model)
    .Name("RolePermissionGrid")
    .ToolBar(toolBar => toolBar.Template(
    @<text>
    @{
        if (Html.HasUiPermission(this.ViewContext, "searchinpermission"))
        {
        <label class="customer-label" for="Customers-input">
            控制器名:
        </label>
        @Html.Partial("_ControllerList")
        <label class="customer-label" for="Customers-input">
            权限系统名:
        </label>
        <input type="text" id="searchPermission" value="权限系统名" data-autocomplete="/role/searchinpermission" />
        <input type='button' value='我要搜索' class='t-button' onclick='searchChange()' />
        }
        if (Html.HasUiPermission(this.ViewContext, "addpermission"))
        { 
        <a href='/role/addpermission/@(ViewBag.rolemodel.Id)' class='t-button'>添加权限</a>
        }
        if (Html.HasUiPermission(this.ViewContext, "muldeleterolepermission"))
        { 
        <a href='javascript:;' class='t-button' onclick='mulDeleteRolePermission()'>删除权限</a>
        }
    }
    <a href='/role/' style="float: right;" class='t-button'>返回</a>
    </text>))
    .Columns(columns =>
    {
        columns.Template(@<text><input name="selectRow" type="checkbox" value="@item.ActionId " title="checkedRecords" /></text>)
        .HeaderTemplate(@<text><input type="checkbox" title="选择所有" id="checkAllRecords" /></text>)
        .Width(50).HeaderHtmlAttributes(new { style = "text-align:center" }).HtmlAttributes(new { style = "text-align:center" })
        .ClientTemplate("<input type=\"checkbox\" title=\"check all records\" name = 'selectRow' value = '<#= ActionId #>'/>");
        columns.Bound(o => o.ActionId).Width(50).Title("系统Id").Hidden();
        columns.Bound(o => o.ActionName).Width(100).Title("权限系统名").ReadOnly();
        columns.Bound(o => o.ActionRemark).Width(200).Title("权限名描述");
        columns.Bound(o => o.ActionClassName).Width(200).Title("权限分类名");
        columns.Bound(o => o.ActionClassId).Width(200).Title("权限分类Id").Hidden();
        columns.Bound(o => o.ControllerClassId).Width(100).Title("控制器分类Id").Hidden();
        columns.Bound(o => o.ControllerClassName).Width(100).Title("控制器分类名").Hidden();
        columns.Bound(o => o.ControllerId).Width(50).Title("所属控制器Id").Hidden();
        columns.Bound(o => o.ControllerName).Width(300).Title("所属控制器系统名").Hidden();
        columns.Template(@<text><a href ="/permission/controllerinfo/@(item.ControllerId)" title = "@(item.ControllerRemark)">
            @(item.ControllerRemark)</a></text>)
        .ClientTemplate("<a href ='/permission/controllerinfo/<#=ControllerId#>' title = '<#= ControllerRemark #>'> <#= ControllerRemark #></a>")
        .Width(300).Title("所属控制器名");
    })
    .ClientEvents(clientEvents => clientEvents.OnDataBinding("dataBinding").OnComplete("bindComplete"))
    .DataKeys(key => key.Add("ActionId"))//添加主键
    .DataBinding(dataBinding => dataBinding.Ajax().Select("ajaxpermission", "role"))
    .Editable(editing => editing.Mode(GridEditMode.InLine))
    .Pageable(page => page.PageSize(15))
    .Groupable()
    .Render();
}
<script type="text/javascript">
    function searchChange() {
        $("#RolePermissionGrid").data("tGrid").rebind();
    }

    function dataBinding(args) {
        var searchKeyStr = $("#searchPermission").val();
        var idStr = $('#RoleId').val();
        var conIdStr = $('#controllerId').val();
        if (searchKeyStr == '权限系统名') {
            searchKeyStr = '';
        }
        args.data = $.extend(args.data, { searchKey: searchKeyStr, id: idStr,conId: conIdStr });
    }

    function mulDeleteRolePermission() {
        var delStr = $(':checked, #RoleId');
        if (delStr.length <= 1) {
            alert('亲，您没有选择任何记录.');
            return;
        }
        jQuery.post('/role/muldeleterolepermission', delStr, function (res) {
            if (res.success) {
                setTimeout(function () {
                    $("#RolePermissionGrid").data("tGrid").rebind();
                    $('#checkAllRecords').attr("checked", false);
                }, 1000);
            } else {
                alert(res.message);
            }
        })
    }

    function bindComplete(e) {
        $("input:checkbox[name='selectRow']").attr("checked", false);
        var success = e.response.success;
        if (success != null && success != undefined) {
            alert(e.response.message);
            window.location.reload();
        }
    }

    $(function () {
        $("input:checkbox[name='selectRow']").attr("checked", false);
        $('#checkAllRecords').click(function checkAll() { $("#RolePermissionGrid tbody input:checkbox[name='selectRow']").attr("checked", this.checked); });
        $(":input[data-autocomplete]").each(function () {
            $(this).autocomplete({
                source: function (request, response) {
                    var url = '/role/searchinpermission/';
                    $.getJSON(url, {
                        term: $.trim(request.term),
                        id: $('#RoleId').val(),
                        conId: $('#controllerId').val()
                    }, response);
                }
            });
        });

        $("#searchPermission").focus(function () {
            if (this.value == "权限系统名") {
                $("#searchPermission").val("");
            }
        }).blur(function () {
            if (this.value == "") {
                $("#searchPermission").val("权限系统名");
            }
        });
    });
</script>
